#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <ctype.h>

/**
 * * Strip left white spaces from a string.
 * * @param str String to parse
 * * @return The new string, without left spaces
 * * @author Original code was contribuition by Erik Jansson
 * * @see rtrim,  trim
 * *
 * * \code
 * * char s[] = "     String with spaces    ";
 * * printf("_%s_\n", s);
 * * ltrim(s);
 * * printf("_%s_\n", s);
 * * \endcode
 * */
void ltrim(char *str)
{
        char *s = str;

        while (isspace(*s))
                s++;

        while ((*str++ = *s++));
}

/**
 * * Strip right white spaces from a string.
 * * @param str String to parse
 * * @return The new string, without left spaces
 * * @author Original code was contribuition by Erik Jansson
 * * @see ltrim,  trim
 * *
 * * \code
 * * char s[] = "     String with spaces    ";
 * * printf("_%s_\n", s);
 * * rtrim(s);
 * * printf("_%s_\n", s);
 * * \endcode
 * */
void rtrim(char *str)
{
        char *s = str;
        register int i;

        for (i = strlen(s)-1; isspace(*(s+i)); i--)
                *(s+i) = '\0';

        while ((*str++ = *s++));
}

/**
 * * Strip both left and right white spaces from a string.
 * * @param str String to parse
 * * @return The new string, without left spaces
 * * @author Original code was contribuition by Erik Jansson
 * * @see ltrim,  trim
 * *
 * * \code
 * * char s[] = "     String with spaces    ";
 * * printf("_%s_\n", s);
 * * trim(s);
 * * printf("_%s_\n", s);
 * * \endcode
 * */
void trim(char *str)
{
        ltrim(str);
        rtrim(str);
}

/**
* Create an array from a string separated by some special char.
* *  Divides the src string in pieces, each delimited by token
* *  and storing the total of pieces in total
* * @param src String to parse
* * @param token Character delimiter to search.
* * @param total An integer variable passed as reference, which stores the total of 
* * itens of the array
* * @return The array, where each item is one separeted by token
* * 
* * \code
* *  
* *  char **pieces;
* *  char *name = "This,is,a,string,of,test";
* *  int total, i;
* *  pieces = explode(name, ',', &total);
* *  for (i = 0; i < total; i++)
* *       printf("Piece %d: %s\n", i, *(pieces+i));
* * \endcode
* **/

char **explode(char *string, char separator, int *arraySize){
    int start = 0, i, k = 1, count = 2;
    char **strarr;
    for (i = 0; string[i] != '\0'; i++){
        /* how many rows do we need for our array? */
        if (string[i] == separator){
            count++;
        }
    }
    arraySize[0]=count-1;
    /* count is at least 2 to make room for the entire string
 *      * and the ending NULL */
    strarr = calloc(count, sizeof(char*));
    i = 0;
    while (*string++ != '\0'){
        if (*string == separator){
            strarr[i] = calloc(k - start + 2,sizeof(char));
            strncpy(strarr[i], string - k + start, k - start);
            strarr[i][k - start + 1] = '\0'; /* guarantee null termination */
            start = k;
            start++;
            i++;
        }
        k++;
    }
    /* copy the last part of the string after the last separator */
    strarr[i] = calloc(k - start,sizeof(char));
    strncpy(strarr[i], string - k + start, k - start - 1);
    strarr[i][k - start - 1] = '\0'; /* guarantee null termination */
    strarr[++i] = NULL;
    
    return strarr;
}

int main(void)
{
    //char s* = "     String with spaces    "; //attention: error
    char s[] = "     String with spaces    ";
    printf("_%s_\n", s);
    ltrim(s);
    printf("_%s_\n", s);
    rtrim(s);
    printf("_%s_\n", s);
    char **pieces;
    char *name = "This,is,a,string,of,test";
    int total, j;
    pieces = explode(name, ',', &total);
    for (j = 0; j < total; j++) {
	    printf("Piece %d: %s\n", j, pieces[j]);
	    free(pieces[j]);
    }
    free(pieces); 

    return 0;
}

